<meta charset="utf-8">
(#) Use `@RequiresApi` instead of `@TargetApi`

!!! WARNING: Use `@RequiresApi` instead of `@TargetApi`
   This is a warning.

Id
:   `UseRequiresApi`
Summary
:   Use `@RequiresApi` instead of `@TargetApi`
Severity
:   Warning
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   8.9.0-alpha04 (December 2024)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/AnnotationDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/AnnotationDetectorTest.kt)

The `@TargetApi` annotation only *suppresses* API warnings locally.
`@RequiresApi` on the other hand will propagate the requirement out to
any *callers* of this API, making sure that they either perform API
level checks (using for example `SDK_INT`), or defining `@RequiresApi`
annotations themselves.

(The `@TargetApi` annotation predates `@RequiresApi`, and was introduced
as an early way to *suppress* lint API warnings for a particular API
level. Accidentally using `@TargetApi` can lead to crashes since there
is no check that other calls to this method actually check that the call
is safe.)

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/WrongUsagesJava.java:5:Warning: Use
@RequiresApi(Build.VERSION_CODES.HONEYCOMB) instead of @TargetApi` to
propagate the requirement to users of WrongUsagesJava [UseRequiresApi]
@TargetApi(Build.VERSION_CODES.HONEYCOMB) // ERROR 1
-----------------------------------------
src/test/pkg/WrongUsagesJava.java:7:Warning: Use @RequiresApi(31)
instead of @TargetApi` to propagate the requirement to callers of
testApi [UseRequiresApi]
    @TargetApi(31) // ERROR 2
    --------------
src/test/pkg/WrongUsagesJava.java:10:Warning: Use @RequiresApi(value =
32) instead of @TargetApi` to propagate the requirement to accessors of
testApi [UseRequiresApi]
    @TargetApi(value = 32) // ERROR 3
    ----------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/test/pkg/WrongUsagesJava.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;
import android.os.Build;
import android.annotation.TargetApi;

@TargetApi(Build.VERSION_CODES.HONEYCOMB) // ERROR 1
public class WrongUsagesJava {
    @TargetApi(31) // ERROR 2
    public void testApi() { }

    @TargetApi(value = 32) // ERROR 3
    public int testApi = 1;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/WrongUsagesKotlin.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg

import android.annotation.TargetApi
import android.os.Build

class WrongUsagesKotlin(
    @field:TargetApi(value = 31) // ERROR 4
    @get:TargetApi(Build.VERSION_CODES.TIRAMISU) // ERROR 5
    val member: String
) {
    @TargetApi(31) // ERROR 6
    fun testApi() {
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/AnnotationDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `AnnotationDetector.testTargetApi`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("UseRequiresApi")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("UseRequiresApi")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection UseRequiresApi
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="UseRequiresApi" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'UseRequiresApi'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore UseRequiresApi ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>